//
//  WBNavigationController.swift
//  赝品微博
//
//  Created by 刘浩林 on 2016/10/31.
//  Copyright © 2016年 刘浩林. All rights reserved.
//

import UIKit

class WBNavigationController: UINavigationController {

    override func viewDidLoad() {
        super.viewDidLoad()


        //隐藏默认的NavigationBar
        navigationBar.isHidden = true

    }
    //重写push方法,所有push动作都会调用此方法
    //viewController是被push的chontroller,设置左侧按钮作为返回按钮
    override func pushViewController(_ viewController: UIViewController, animated: Bool) {

        //如果不是栈底控制器才需要隐藏,根控制器不需要处理
        if childViewControllers.count > 0 {
            viewController.hidesBottomBarWhenPushed = true
            //判断控制器类型
            if let vc = viewController as? WBBaseViewController {

                var title = "返回"
                //判断控制器级数 只有一个子控制器时候,显示栈底控制器的标题
                if childViewControllers.count == 1 {
                    //titile显示首页的标题
                    title = childViewControllers.first?.title ?? "返回"
                }




                //取出自定义的 navItem 设置左侧按钮作为返回按钮
                vc.navItem.leftBarButtonItem = UIBarButtonItem(title: title, target: self, action: #selector(popToParent),isBack:true)
                
            }

        }






        super.pushViewController(viewController, animated: true)



    }


   @objc func popToParent() -> () {
        popViewController(animated: true)
    }


}
